﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Wpftest.AttributeStudy
{
    /// <summary>
    /// MyDependenceProperty.xaml 的交互逻辑
    /// </summary>
    public partial class MyDependenceProperty : UserControl
    {
        public MyDependenceProperty()
        {
            InitializeComponent();
        }
        //定义依赖属性变量
        public static readonly DependencyProperty MyColorProperty;
        //注册依赖属性
        static MyDependenceProperty()
        {
            MyColorProperty = DependencyProperty.Register("MyColor", typeof(string), typeof(MyDependenceProperty), new PropertyMetadata("Red", (s, e) => {
                var mdp = s as MyDependenceProperty;
                if (mdp != null)
                {
                    try
                    {
                        var color = (Color)ColorConverter.ConvertFromString(e.NewValue.ToString());
                        mdp.Foreground = new SolidColorBrush(color);
                    }
                    catch
                    {

                        mdp.Foreground = new SolidColorBrush(Colors.Black);
                    }
                }
            }));
        }
        //用.net属性进行属性包装
        public string MyColor
        {
            get
            {
                return (string)GetValue(MyColorProperty);
            }
            set
            {
                SetValue(MyColorProperty, value);
            }
        }


        //简单创建部分代码的方式,输入propdp,然后点击两次tab键，然后进行修改


        //public string MyColor
        //{
        //    get { return (string)GetValue(MyColorProperty); }
        //    set { SetValue(MyColorProperty, value); }
        //}

        //// Using a DependencyProperty as the backing store for MyColor.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty MyColorProperty =
        //    DependencyProperty.Register("MyColor", typeof(string), typeof(MyDependenceProperty), new PropertyMetadata("Red", (s, e) => { 

        //    }));

        //附加属性部分代码，附加属性定义过程：声明附加属性变量；在属性系统中进行注册；调用静态方法设置和获取属性值；


        public static int GetMyAttachedFontSize(DependencyObject obj)
        {
            return (int)obj.GetValue(MyAttachedFontSizeProperty);
        }

        public static void SetMyAttachedFontSize(DependencyObject obj, int value)
        {
            obj.SetValue(MyAttachedFontSizeProperty, value);
        }

        // Using a DependencyProperty as the backing store for MyAttachedFontSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyAttachedFontSizeProperty =
            DependencyProperty.RegisterAttached("MyAttachedFontSize", typeof(int), typeof(MyDependenceProperty), new PropertyMetadata((s, e) => {
                var mdp = (((((s as FrameworkElement).Parent) as FrameworkElement).Parent) as FrameworkElement).Parent as MyDependenceProperty;
                if (mdp != null&&e.NewValue!=null)
                {
                    var fontsize = 20;
                    int.TryParse(e.NewValue.ToString(), out fontsize);
                    mdp.FontSize = fontsize;
                }
            }));


    }
}
